package com.yanqu.road.server.manger.activity.cookboy;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.cookboy.CookBoyData;
import com.yanqu.road.entity.activity.cookboy.CookBoyFinishFood;
import com.yanqu.road.entity.activity.cookboy.MakeFoodRes;
import com.yanqu.road.entity.activity.cookboy.process2.CookBoy2Dish;
import com.yanqu.road.entity.activity.cookboy.process2.PickDishRes;
import com.yanqu.road.entity.enums.activity.eActivityCookBoyType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.pb.cookboy.CookBoyProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.RelationActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * 小当家
 */
public class CookBoyMgr extends TempMgr {
    private static Logger logger = LogManager.getLogger(CookBoyMgr.class.getName());

    //活动id
    private static Map<Integer, CookBoy> cookBoyMap = new ConcurrentHashMap<>();//活动map
    private static Map<Integer, CookBoy2> cookBoy2Map = new ConcurrentHashMap<>();//活动map
    private static Map<Integer, CookBoy3> cookBoy3Map = new ConcurrentHashMap<>();//活动map

    private static Map<Integer, Object> activityLock;

    public static void syncCookBoyWindUp(int activityId, List<Long> userIdList) {
        Map<Long, CookBoyData> windUpUserCookBoy1Map = new ConcurrentHashMap<>();
        synchronized (getActivityLock(activityId)) {
            CookBoy cookBoy = getCookBoy(activityId);
            if (cookBoy != null) {
                windUpUserCookBoy1Map = cookBoy.getWindUpUserCookBoyMap(userIdList);
            }
        }
        ActivityInfo ck1Ac = CookBoyActivityMgr.getOpenActivityInfo(activityId);
        List<ActivityInfo> activityInfoList = CookBoyActivityMgr.getOpenActivityInfoList(eActivityType.CookingBoyActivity.getValue(), eActivityCookBoyType.Process2.getChildType());
        for (ActivityInfo activityInfo : activityInfoList) {
            //阶段2活动和阶段1活动有重叠或者只有一个活动
            if (CookBoyActivityMgr.isActivityContain(ck1Ac, activityInfo)) {
                if (activityInfo.getServerIdList().contains(GameServer.getInstance().getServerId())) {
                    synchronized (getActivityLock(activityInfo.getActivityId())) {
                        CookBoy2 cookBoy2 = getCookBoy2(activityInfo.getActivityId());
                        if (cookBoy2 != null) {
                            cookBoy2.addPromotionUserList(activityId, userIdList, windUpUserCookBoy1Map);
                        }
                    }
                }
            }
        }
    }

    public static void syncCookBoy2WindUp(int activityId, List<Long> userIdList) {
        Map<Long, CookBoyData> windUpUserCookBoy2Map = new ConcurrentHashMap<>();
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                windUpUserCookBoy2Map = cookBoy2.getWindUpUserCookBoyMap(userIdList);
            }
        }
        ActivityInfo ck1Ac = CookBoyActivityMgr.getOpenActivityInfo(activityId);
        List<ActivityInfo> activityInfoList = CookBoyActivityMgr.getOpenActivityInfoList(eActivityType.CookingBoyActivity.getValue(), eActivityCookBoyType.Process3.getChildType());
        for (ActivityInfo activityInfo : activityInfoList) {
            //阶段2活动和阶段1活动有重叠或者只有一个活动
            if (CookBoyActivityMgr.isActivityContain(ck1Ac, activityInfo)) {
                if (activityInfo.getServerIdList().contains(GameServer.getInstance().getServerId())) {
                    synchronized (getActivityLock(activityInfo.getActivityId())) {
                        CookBoy3 cookBoy3 = getCookBoy3(activityInfo.getActivityId());
                        if (cookBoy3 != null) {
                            cookBoy3.addPromotionUserList(userIdList, windUpUserCookBoy2Map);
                        }
                    }
                }
            }
        }
    }

    /*晋级↑↑↑↑↑↑*/

    public static void loadActivity(int activityId) {
        synchronized (getActivityLock(activityId)) {
            ActivityInfo acInfo = CookBoyActivityMgr.getOpenActivityInfo(activityId);
            if (acInfo != null) {
                if(acInfo.getChildType() == eActivityCookBoyType.Process1.getChildType()){
                    if (!cookBoyMap.containsKey(activityId)) {
                        CookBoy cookBoy = new CookBoy(acInfo);
                        if (cookBoy.init()) {
                            cookBoyMap.put(activityId, cookBoy);
                        } else {
                            logger.error("小当家活动" + activityId + "加载失败");
                        }
                    }
                }else if(acInfo.getChildType() == eActivityCookBoyType.Process2.getChildType()){
                    if (!cookBoy2Map.containsKey(activityId)) {
                        CookBoy2 cookBoy2 = new CookBoy2(acInfo);
                        if (cookBoy2.init()) {
                            cookBoy2Map.put(activityId, cookBoy2);
                        } else {
                            logger.error("小当家2活动" + activityId + "加载失败");
                        }
                    }
                }else if(acInfo.getChildType() == eActivityCookBoyType.Process3.getChildType()){
                    if (!cookBoy3Map.containsKey(activityId)) {
                        CookBoy3 cookBoy3 = new CookBoy3(acInfo);
                        if (cookBoy3.init()) {
                            cookBoy3Map.put(activityId, cookBoy3);
                        } else {
                            logger.error("小当家3活动" + activityId + "加载失败");
                        }
                    }
                }
            }
        }
    }

    public static void openBanquetAddCondition(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy3 cookBoy3 = getCookBoy3(activityId);
            if (cookBoy3 != null) {
                cookBoy3.openBanquetAddCondition(userId);
            }
        }
    }

    public static int sendOpenTable(int activityId, long userId, int itemId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy3 cookBoy3 = getCookBoy3(activityId);
            if (cookBoy3 != null) {
                return cookBoy3.sendOpenTable(userId, itemId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static int sendCooker3Msg(int activityId, long userId, long homeUserId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy3 cookBoy3 = getCookBoy3(activityId);
            if (cookBoy3 != null) {
                return cookBoy3.sendCookerMsg(userId, homeUserId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static int worship3(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy3 cookBoy3 = getCookBoy3(activityId);
            if (cookBoy3 != null) {
                return cookBoy3.worship(userId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static void syncCookBoy3Table(int activityId, CookBoyProto.CrossCookBoy3TableSyncMsg msg) {
        synchronized (getActivityLock(activityId)) {
            CookBoy3 cookBoy3 = getCookBoy3(activityId);
            if (cookBoy3 != null) {
                cookBoy3.syncCookBoy3Table(msg);
            }
        }
    }

    public static void syncCookBoy3BanquetUserData(int activityId, CookBoyProto.CookBoy3UserRewardSyncMsg msg) {
        synchronized (getActivityLock(activityId)) {
            CookBoy3 cookBoy3 = getCookBoy3(activityId);
            if (cookBoy3 != null) {
                cookBoy3.syncCookBoy3BanquetUserData(msg);
            }
        }
    }

    public static void syncCookBoy3BanquetOpenNotice(int activityId, CookBoyProto.CookBoy3BanquetOpenSyncMsg msg) {
        synchronized (getActivityLock(activityId)) {
            CookBoy3 cookBoy3 = getCookBoy3(activityId);
            if (cookBoy3 != null) {
                cookBoy3.syncCookBoy3BanquetOpenNotice(msg);
            }
        }
    }

    //参加宴会
    public static int sendJoinBanquetMsg(int activityId, long userId, long homeUserId, int banquetId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy3 cookBoy3 = getCookBoy3(activityId);
            if (cookBoy3 != null) {
                return cookBoy3.sendJoinBanquetMsg(userId, homeUserId, banquetId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static void cookBoy3openerBackMsg(int activityId, long userId, int banquetItemId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy3 cookBoy3 = getCookBoy3(activityId);
            if (cookBoy3 != null) {
                cookBoy3.cookBoy3OpenerBackMsg(userId, banquetItemId);
            }
        }
    }

    public static String cookBoy3joinerBackMsg(int activityId, long userId, int banquetItemId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy3 cookBoy3 = getCookBoy3(activityId);
            if (cookBoy3 != null) {
                return cookBoy3.CookBoy3joinerBackMsg(userId, banquetItemId);
            }
        }
        return "";
    }

    public static void cookBoy3RobotBackMsg(int activityId, long userId, long integral) {
        synchronized (getActivityLock(activityId)) {
            CookBoy3 cookBoy3 = getCookBoy3(activityId);
            if (cookBoy3 != null) {
                cookBoy3.cookBoy3RobotBackMsg(userId, integral);
            }
        }
    }

    /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓小当家2↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
    public static void cookBoy2RobotBackMsg(int activityId, CookBoyProto.CrossCookBoy2RobotJoinBanquetMsg msg) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.cookBoy2RobotBackMsg(msg);
            }
        }
    }

    public static void syncHorseNotice(int activityId, CookBoyProto.CrossCookBoy2HorseSyncServerMsg msg) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.syncHorseNotice(msg);
            }
        }
    }

    public static void syncTable(int activityId, CookBoyProto.CrossCookBoy2TableSyncMsg msg) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.syncTable(msg);
                return;
            }
        }
        //同步过来的是赴宴范围的桌子
        OtherActivityCookBoy2 cookBoy2 = OtherActivityBoyActivityMgr.getOtherActivityCookBoy2(activityId);
        if(cookBoy2 != null){
            cookBoy2.syncTable(msg);
        }else {
            getLogger().info("小当家2阶段，赴宴范围活动{}，缺失活动基础配置", activityId);
        }
    }

    public static void syncFoodServe(int activityId, long userId, CookBoyProto.CookBoy2OpenTableRespMsg msg) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.syncFoodServe(userId, msg);
            }
        }
    }

    public static void backFoodServe(int activityId, long userId, CookBoyProto.CookBoy2OpenTableRespMsg msg) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.backFoodServe(userId, msg);
            }
        }
    }

    public static void syncCooker(int activityId, CookBoyProto.CrossCookBoy2CookerSyncMsg msg) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.syncCooker(msg);
                return;
            }
        }
        //同步过来的是赴宴范围的桌子
        OtherActivityCookBoy2 cookBoy2 = OtherActivityBoyActivityMgr.getOtherActivityCookBoy2(activityId);
        if(cookBoy2 != null){
            cookBoy2.syncCooker(msg);
        }else {
            getLogger().info("小当家2阶段，赴宴范围活动{}，缺失活动基础配置", activityId);
        }
    }

    public static void openerBackMsg(int activityId, long userId, int spoonId, int tableId, int autoJoin) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.openerBackMsg(userId, spoonId, tableId, autoJoin);
            }
        }
    }

    public static String joinerBackMsg(int activityId, long userId, int spoonId, int autoJoin) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.joinerBackMsg(userId, spoonId, autoJoin);
            }
        }
        return "";
    }

    public static void sendCookerHomeMsg(int activityId, long userId, long homeUser, int page, int num) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.sendCookerHomeMsg(userId, homeUser, page, num);
                return;
            }
        }
        //操作赴宴范围的桌子
        OtherActivityCookBoy2 cookBoy2 = OtherActivityBoyActivityMgr.getOtherActivityCookBoy2(activityId);
        if(cookBoy2 != null){
            cookBoy2.sendCookerHomeMsg(userId, homeUser, page, num);
        }
    }

    public static void sendCookerTableMsg(int activityId, long userId, long homeUser, int tableId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.sendCookerTableMsg(userId, homeUser, tableId);
            }
        }
    }

    //参加宴会
    public static int sendJoinMsg(int activityId, long userId, long honeUserId, int tableId, int spoonId, int trueActivityId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.sendJoinMsg(userId, honeUserId, tableId, spoonId, trueActivityId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    //上菜开桌子
    public static int sendServeDish(int activityId, long userId, List<Integer> dishIdList) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.sendServeDish(userId, dishIdList);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static int sendForbidSpoon(int activityId, long userId, List<Integer> spoonIdList) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.sendForbidSpoon(userId, spoonIdList);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static int sendCookerMsg(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.sendCookerMsg(userId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static int sendRewardMsg(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.sendRewardMsg(userId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static int sendRelationMsg(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.sendRelationMsg(userId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static int sendSearchUserMsg(int activityId, long userId, String searchId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.sendSearchUserMsg(userId, searchId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static PickDishRes pickDish(int activityId, long userId, int times) {
        PickDishRes pickDishRes = new PickDishRes();
        pickDishRes.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.newPickDish(userId, times);
            }
        }
        return pickDishRes;
    }

    public static CookBoy2Dish dishMerge(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.dishMerge(userId);
            }
        }
        return null;
    }

    public static void addCookBoy2SceneUser(int activityId, long userId, long openBanquetUserId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.addSceneUser(userId, openBanquetUserId);
                return;
            }
        }
        //操作赴宴范围的桌子
        OtherActivityCookBoy2 cookBoy2 = OtherActivityBoyActivityMgr.getOtherActivityCookBoy2(activityId);
        if(cookBoy2 != null){
            cookBoy2.addSceneUser(userId, openBanquetUserId);
        }
    }

    public static void removeCookBoy2SceneUser(int activityId, long userId, long openBanquetUserId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.removeSceneUser(userId, openBanquetUserId);
            }
        }
    }

    public static int worship2(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.worship(userId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }
    /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓小当家1↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

    public static void recoverHp(int activityId, long userId, long count) {
        synchronized (getActivityLock(activityId)) {
            CookBoy cookBoy = getCookBoy(activityId);
            if (cookBoy != null) {
                cookBoy.recoverHp(userId, count);
            }
        }
    }

    public static int worship(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy cookBoy = getCookBoy(activityId);
            if (cookBoy != null) {
                return cookBoy.worship(userId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    public static void addSceneUser(int activityId, long userId, long openBanquetUserId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy cookBoy = getCookBoy(activityId);
            if (cookBoy != null) {
                cookBoy.addSceneUser(userId, openBanquetUserId);
            }
        }
    }

    public static void removeSceneUser(int activityId, long userId, long openBanquetUserId) {
        ActivityInfo activityInfo = RelationActivityMgr.getActivityInfo(activityId);
        if (activityInfo.getChildType() == eActivityCookBoyType.Process1.getChildType()) {
            synchronized (getActivityLock(activityId)) {
                CookBoy cookBoy = getCookBoy(activityId);
                if (cookBoy != null) {
                    cookBoy.removeSceneUser(userId, openBanquetUserId);
                }
            }
        } else if (activityInfo.getChildType() == eActivityCookBoyType.Process2.getChildType()) {
            synchronized (getActivityLock(activityId)) {
                CookBoy2 cookBoy2 = getCookBoy2(activityId);
                if (cookBoy2 != null) {
                    cookBoy2.removeSceneUser(userId, openBanquetUserId);
                }
            }
        } else if (activityInfo.getChildType() == eActivityCookBoyType.Process3.getChildType()) {
            synchronized (getActivityLock(activityId)) {
                CookBoy3 cookBoy3 = getCookBoy3(activityId);
                if (cookBoy3 != null) {
                    cookBoy3.removeSceneUser(userId, openBanquetUserId);
                }
            }
        }

    }


    public static void updateMatchOverFood(int activityId, CookBoyProto.CrossCookBoyMatchOverRewardSyncMsg msg) {
        synchronized (getActivityLock(activityId)) {
            CookBoy cookBoy = getCookBoy(activityId);
            if (cookBoy != null) {
                cookBoy.updateMatchOverFood(msg);
            }
        }
    }

    public static void syncMatch(int activityId, CookBoyProto.CrossCookBoyMatchSyncMsg msg) {
        synchronized (getActivityLock(activityId)) {
            CookBoy cookBoy = getCookBoy(activityId);
            if (cookBoy != null) {
                cookBoy.syncMatch(msg);
            }
        }
    }

    public static CookBoyData getUserCookBoyData(int activityId, GamePlayer player) {
        synchronized (getActivityLock(activityId)) {
            CookBoy cookBoy = getCookBoy(activityId);
            if (cookBoy != null) {
                return cookBoy.getUserCookBoyData(player);
            }
        }
        return null;
    }

    public static CookBoyData getUserCookBoy2DataNoLock(int activityId, GamePlayer player) {
        CookBoy2 cookBoy2 = getCookBoy2NoInit(activityId);
        if (cookBoy2 != null) {
            return cookBoy2.getUserCookBoyData(player);
        }
        return null;
    }

    //加锁用户登陆会有GamePlayerMgr.getPlayerLock锁和getActivityLock(activityId)锁，timerTask会有getActivityLock(activityId)锁和发邮件的GamePlayerMgr.getPlayerLock锁
    public static CookBoyData getUserCookBoy2Data(int activityId, GamePlayer player) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.getUserCookBoyData(player);
            }
        }
        return null;
    }

    public static CookBoyData getUserCookBoy3Data(int activityId, GamePlayer player) {
        synchronized (getActivityLock(activityId)) {
            CookBoy3 cookBoy3 = getCookBoy3(activityId);
            if (cookBoy3 != null) {
                return cookBoy3.getUserCookBoyData(player);
            }
        }
        return null;
    }

    public static void setUserCookBoy3Able(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy3 cookBoy3 = getCookBoy3(activityId);
            if (cookBoy3 != null) {
                cookBoy3.setUserCookBoy3Able(userId);
            }
        }
    }

    public static boolean isUserCookBoyAble(int activityId, long userId) {
        ActivityInfo activityInfo = RelationActivityMgr.getOpenActivityInfo(activityId);
        if (activityInfo != null) {
            synchronized (getActivityLock(activityId)) {
                if (activityInfo.getChildType() == eActivityCookBoyType.Process1.getChildType()) {
                    CookBoy cookBoy = getCookBoy(activityId);
                    if (cookBoy != null) {
                        return cookBoy.isUserCookBoyAble(userId);
                    }
                } else if (activityInfo.getChildType() == eActivityCookBoyType.Process2.getChildType()) {
                    CookBoy2 cookBoy2 = getCookBoy2(activityId);
                    if (cookBoy2 != null) {
                        return cookBoy2.isUserCookBoy2Able(userId);
                    }
                } else if (activityInfo.getChildType() == eActivityCookBoyType.Process3.getChildType()) {
                    CookBoy3 cookBoy3 = getCookBoy3(activityId);
                    if (cookBoy3 != null) {
                        return cookBoy3.isUserCookBoy3Able(userId);
                    }
                }
            }
        }
        return false;
    }

    public static MakeFoodRes makeFood(int activityId, long userId, int times) {
        MakeFoodRes res = new MakeFoodRes();
        synchronized (getActivityLock(activityId)) {
            CookBoy cookBoy = getCookBoy(activityId);
            if (cookBoy != null) {
                return cookBoy.makeFood(userId, times);
            }
        }
        res.setRet(GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME);
        return res;
    }

    public static CookBoyData serveFoodRespond(int activityId, long userId, int no, int index) {
        synchronized (getActivityLock(activityId)) {
            CookBoy cookBoy = getCookBoy(activityId);
            if (cookBoy != null) {
                return cookBoy.serveFoodRespond(userId, no, index);
            }
        }
        return null;
    }

    public static CookBoyFinishFood foodMerge(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy cookBoy = getCookBoy(activityId);
            if (cookBoy != null) {
                return cookBoy.foodMerge(userId);
            }
        }
        return null;
    }

    private static CookBoy getCookBoy(int activityId) {
        if (cookBoyMap.containsKey(activityId)) {
            return cookBoyMap.get(activityId);
        } else {
            ActivityInfo acInfo = RelationActivityMgr.getOpenActivityInfo(activityId);
            if (acInfo != null) {
                CookBoy cookBoy = new CookBoy(acInfo);
                if (cookBoy.init()) {
                    cookBoyMap.put(activityId, cookBoy);
                } else {
                    logger.error("小当家活动" + activityId + "加载失败");
                }
                return cookBoyMap.get(activityId);
            }
        }
        return null;
    }

    private static CookBoy2 getCookBoy2NoInit(int activityId) {
        if (cookBoy2Map.containsKey(activityId)) {
            return cookBoy2Map.get(activityId);
        }
        return null;
    }

    private static CookBoy2 getCookBoy2(int activityId) {
        if (cookBoy2Map.containsKey(activityId)) {
            return cookBoy2Map.get(activityId);
        } else {
            ActivityInfo acInfo = RelationActivityMgr.getOpenActivityInfo(activityId);
            if (acInfo != null) {
                CookBoy2 cookBoy2 = new CookBoy2(acInfo);
                if (cookBoy2.init()) {
                    cookBoy2Map.put(activityId, cookBoy2);
                } else {
                    logger.error("小当家2活动" + activityId + "加载失败");
                }
                return cookBoy2Map.get(activityId);
            }
        }
        return null;
    }

    private static CookBoy3 getCookBoy3(int activityId) {
        if (cookBoy3Map.containsKey(activityId)) {
            return cookBoy3Map.get(activityId);
        } else {
            ActivityInfo acInfo = RelationActivityMgr.getOpenActivityInfo(activityId);
            if (acInfo != null) {
                CookBoy3 cookBoy3 = new CookBoy3(acInfo);
                if (cookBoy3.init()) {
                    cookBoy3Map.put(activityId, cookBoy3);
                } else {
                    logger.error("小当家3活动" + activityId + "加载失败");
                }
                return cookBoy3Map.get(activityId);
            }
        }
        return null;
    }

    /**
     * 获取活动锁
     *
     * @param activityId 活动ID
     * @return
     */
    private static synchronized Object getActivityLock(int activityId) {
        if (activityLock == null) {
            activityLock = new ConcurrentHashMap<>();
        }
        if (!activityLock.containsKey(activityId)) {
            activityLock.put(activityId, new Object());
        }
        return activityLock.get(activityId);
    }

    private static void removeCookBoyData(int activityId) {
        cookBoyMap.remove(activityId);
        activityLock.remove(activityId);
        logger.error("remove cookBoy activity:{}", activityId);
    }

    private static void removeCook2BoyData(int activityId) {
        cookBoy2Map.remove(activityId);
        activityLock.remove(activityId);
        logger.error("remove cookBoy2 activity:{}", activityId);
    }

    private static void removeCook3BoyData(int activityId) {
        cookBoy3Map.remove(activityId);
        activityLock.remove(activityId);
        logger.error("remove cookBoy3 activity:{}", activityId);
    }

    public static void removeActivity() {
        Map<Integer, ActivityInfo> activityInfoMap = RelationActivityMgr.getOpenActivityInfoMap(eActivityType.CookingBoyActivity.getValue());
        for (CookBoy cookBoy : cookBoyMap.values()) {
            if (!activityInfoMap.containsKey(cookBoy.getActivityInfo().getActivityId())) {
                removeCookBoyData(cookBoy.getActivityInfo().getActivityId());
            }
        }
        for (CookBoy2 cookBoy2 : cookBoy2Map.values()) {
            if (!activityInfoMap.containsKey(cookBoy2.getActivityInfo().getActivityId())) {
                removeCook2BoyData(cookBoy2.getActivityInfo().getActivityId());
            }
        }
        for (CookBoy3 cookBoy3 : cookBoy3Map.values()) {
            if (!activityInfoMap.containsKey(cookBoy3.getActivityInfo().getActivityId())) {
                removeCook3BoyData(cookBoy3.getActivityInfo().getActivityId());
            }
        }
    }

    private static void reloadCookBoy(int activityId) {
        ActivityInfo acInfo = RelationActivityMgr.getOpenActivityInfo(activityId);
        if (acInfo != null) {
            if (cookBoyMap.containsKey(activityId)) {
                cookBoyMap.get(activityId).setActivityInfo(acInfo);
            } else {
                CookBoy cookBoy = new CookBoy(acInfo);
                if (cookBoy.init()) {
                    cookBoyMap.put(activityId, cookBoy);
                } else {
                    logger.error("小当家活动" + activityId + "加载失败");
                }
            }
        }
    }

    private static void reloadCookBoy2(int activityId) {
        ActivityInfo acInfo = RelationActivityMgr.getOpenActivityInfo(activityId);
        if (acInfo != null) {
            if (cookBoy2Map.containsKey(activityId)) {
                cookBoy2Map.get(activityId).setActivityInfo(acInfo);
            } else {
                CookBoy2 cookBoy2 = new CookBoy2(acInfo);
                if (cookBoy2.init()) {
                    cookBoy2Map.put(activityId, cookBoy2);
                } else {
                    logger.error("小当家2活动" + activityId + "加载失败");
                }
            }
        }
    }

    private static void reloadCookBoy3(int activityId) {
        ActivityInfo acInfo = RelationActivityMgr.getOpenActivityInfo(activityId);
        if (acInfo != null) {
            if (cookBoy3Map.containsKey(activityId)) {
                cookBoy3Map.get(activityId).setActivityInfo(acInfo);
            } else {
                CookBoy3 cookBoy3 = new CookBoy3(acInfo);
                if (cookBoy3.init()) {
                    cookBoy3Map.put(activityId, cookBoy3);
                } else {
                    logger.error("小当家3活动" + activityId + "加载失败");
                }
            }
        }
    }

    public static void reloadActivity() {
        Map<Integer, ActivityInfo> activityInfoMap = RelationActivityMgr.getOpenActivityInfoMap(eActivityType.CookingBoyActivity.getValue());
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            if (activityInfo.getChildType() == eActivityCookBoyType.Process1.getChildType()) {
                reloadCookBoy(activityInfo.getActivityId());
            } else if (activityInfo.getChildType() == eActivityCookBoyType.Process2.getChildType()) {
                reloadCookBoy2(activityInfo.getActivityId());
            } else if (activityInfo.getChildType() == eActivityCookBoyType.Process3.getChildType()) {
                reloadCookBoy3(activityInfo.getActivityId());
            }
        }
    }

    //定时处理，活动到期菜品转换成兑换积分
    public static void timerTask() {
        for (CookBoy2 cookBoy2 : cookBoy2Map.values()) {
            synchronized (getActivityLock(cookBoy2.getActivityInfo().getActivityId())) {
                cookBoy2.timerTask();
            }
        }

    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean save() {
        for (CookBoy cookBoy : cookBoyMap.values()) {
            cookBoy.save();
        }
        for (CookBoy2 cookBoy2 : cookBoy2Map.values()) {
            cookBoy2.save();
        }
        for (CookBoy3 cookBoy3 : cookBoy3Map.values()) {
            cookBoy3.save();
        }
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    public static Map<Integer, CookBoy2> getCookBoy2Map() {
        return cookBoy2Map;
    }

    /**
     * 获取自动回访记录
     *
     * @param activityId 活动ID
     * @param userId     用户ID
     * @return ret
     */
    public static int getAutoJoinRecord(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.getAutoJoinRecord(userId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    /**
     * 设置自动回访汤勺
     *
     * @param activityId 活动ID
     * @param userId     用户ID
     * @param spoons     汤勺
     * @return ret
     */
    public static int setAutoJoinSpoon(int activityId, long userId, List<CookBoyProto.CookBoy2SpoonTempMsg> spoons) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.setAutoJoinSpoon(activityId, userId, spoons);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    /**
     * 获取自动回访汤勺
     *
     * @param activityId 活动ID
     * @param userId     用户ID
     * @return ret
     */
    public static int getAutoJoinSpoon(int activityId, long userId) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                return cookBoy2.getAutoJoinSpoon(activityId, userId);
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }

    /**
     * 处理自动回访剩余的汤勺
     *
     * @param activityId 活动ID
     * @param userId     用户ID
     * @param records    回访记录
     */
    public static void dealAutoJoinSpoonRest(int activityId, long userId, List<CookBoyProto.CookBoy2SpoonTempMsg> records) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.dealAutoJoinSpoonRest(userId, records);
            }
        }
    }

    /**
     * 开始自动回访流程
     *
     * @param activityId 活动ID
     * @param reqMsg     reqMsg
     */
    public static void startAutoJoin(int activityId, CookBoyProto.CrossCookBoy2AutoJoinInfoMsg reqMsg) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.startAutoJoin(reqMsg);
            }
        }
    }

    /**
     * 正式进行试吃
     *
     * @param activityId 活动ID
     */
    public static void autoJoinTaste(int activityId, CookBoyProto.CrossCookBoy2SyncTasteTableCntMsg reqMsg) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.autoJoinTaste(reqMsg);
            }
        }
    }

    /**
     * 同步自动回访结果
     *
     * @param activityId 活动ID
     * @param reqMsg     reqMsg
     */
    public static void dealAutoJoinResult(int activityId, CookBoyProto.CrossCookBoy2SyncAutoJoinResultMsg reqMsg) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.dealAutoJoinResult(reqMsg);
            }
        }
    }


    /**
     * 处理自动回访收益
     *
     * @param activityId 活动ID
     * @param userId     用户ID
     * @param returnStr  回访收益
     * @param spoonId    汤勺ID
     * @param count      回访次数
     */
    public static void dealAutoJoinReward(int activityId, long userId, String returnStr, int spoonId, int count) {
        synchronized (getActivityLock(activityId)) {
            CookBoy2 cookBoy2 = getCookBoy2(activityId);
            if (cookBoy2 != null) {
                cookBoy2.dealAutoJoinReward(userId, returnStr, spoonId, count);
            }
        }
    }
}
